package com.jt.common.util;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

public class JsonUtil {

    private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";  
    private static final ObjectMapper mapper;  
      
    public ObjectMapper getMapper() {  
            return mapper;  
    }  

    static {  
            SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);  
              
            mapper = new ObjectMapper();  
            mapper.setDateFormat(dateFormat);
            mapper.setSerializationInclusion(Include.NON_NULL);
    }  
      
    public static String toJson(Object obj) {  
            try {  
                    return mapper.writeValueAsString(obj);  
            } catch (Exception e) {  
                    throw new RuntimeException("转换json字符失败!");  
            }  
    }  
    
    public static <T> T jsonToObject(String json,String key , Class<T> clazz) {
    	  try {  
    		  JsonNode rootNode = mapper.readValue(json, JsonNode.class);  
              JsonNode path = rootNode.path(key);
              if (!path.isMissingNode()) {
            	  return jsonToObject(path.toString(), clazz);
              }
      } catch (IOException e) {  
              throw new RuntimeException("将json字符转换为对象时失败!");  
      }  
    	  return null;
    }
    
  
      
    public static <T> T jsonToObject(String json,Class<T> clazz) {  
            try {  
                    return mapper.readValue(json, clazz);  
            } catch (IOException e) {  
            	e.printStackTrace();
                    throw new RuntimeException("将json字符转换为对象时失败!");  
            }  
    }  
      
    public static class JsonDateSerializer extends JsonSerializer<Date>{  
        private SimpleDateFormat dateFormat;  
        public JsonDateSerializer(String format) {  
             dateFormat = new SimpleDateFormat(format);  
            }  
          
        @Override  
        public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)  
                throws IOException, JsonProcessingException {  
            String value = dateFormat.format(date);  
            gen.writeString(value);  
        }  
    }  
    
    
 // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
    	try {
			String string = MAPPER.writeValueAsString(data);
			return string;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
    	return null;
    }

    /**
     * 将json结果集转化为对象
     * 
     * @param jsonData json数据
     * @param clazz 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, new TypeReference<T>() {
			});
            return t;
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
    	MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		List<T> list = MAPPER.readValue(jsonData, javaType);
    		return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    }

   
}
